<?php
class CV_ORM_Schema_Writer_SeparateFile_ArrayAccess{
	protected $pdo;
	public function __construct( $pdo ){
		$this->pdo = $pdo;
	}
	public function get( $id ){
		return $this->sqlSelectOne( $this->getPrimaryKey()." = ?", $id );
	}
	public function update( $array ){
		return $this->sqlUpdate( $array, $this->getPrimaryKey()." = ?", $id );
	}
	public function insert( $object ){
		/* $array = $object->_getState();
		 $this->dataAccess->insert( $array );*/
	}
	protected function mapArrayToProperties( $array ){
		return $array;
	}
	protected function getPrimaryKey(){
		return $this->primaryKey;
	}
	public function sqlSelectOne( $where, $args = array() ){
		return $this->sqlSelect( $where, $args )->fetch();
	}
	public function sqlSelectMany( $where, $args = array() ){
		return $this->sqlSelect( $where, $args )->fetchAll();
	}
	protected function sqlSelect( $where, $args = array() ){
		if( !is_array($args) ){
			$args = array( $args );
		}
		$sql = 'SELECT * FROM `'.implode("`.`",array($this->database,$this->table)).'` WHERE '.$where;
		$statement = $this->pdo->prepare( $sql  );
		$statement->execute( $args );
		return $statement;
	}
	protected function sqlUpdate( $array, $where, $args = array() ){
		if( !is_array($args) ){
			$args = array( $args );
		}
		$sql = 'UPDATE `'.implode("`.`",array($this->database,$this->table)).'` SET `'.implode("`=?,`",array_keys($array)).'`=? WHERE '.$where;

		$statement = $this->pdo->prepare( $sql );
		$statement->execute( array_merge(array_values($array), $args) );
		return $statement;
	}
}